
<template>
  <div class="users">
    <el-breadcrumb separator="/" class="bread">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">用户管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <div style="margin-top: 15px;">
      <el-input placeholder="请输入内容" v-model="searchVal" class="search-input mb">
          <el-button slot="append" icon="el-icon-search" @click="handleSearch"></el-button>
      </el-input>
       <el-button type="primary" plain @click="handleAdd">添加用户</el-button>
    </div>
    <el-table
      :data="tableData"
      border
       v-loading="loading"
      style="width: 100%;">
      <el-table-column
        type="index"
        width="50">
      </el-table-column>
      <el-table-column
        prop="username"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱"
        width="180">
      </el-table-column>
      <el-table-column
        prop="mobile"
        label="电话">
      </el-table-column>
      <el-table-column
        prop="mg-state"
        label="状态">
        <template slot-scope="">
          <el-switch
            v-model="value2"
            active-color="#13ce66"
            inactive-color="#c7c7c7">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop=""
        label="操作">
        <template slot-scope="scope">
           <!-- scope.row获取到的是你点击的那一行对象的所有数据 -->
          <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="editUser(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="deleteList(scope.row)"></el-button>
          <el-button type="success" icon="el-icon-check" circle size="mini"></el-button>
        </template>
      </el-table-column>
    </el-table>
<el-dialog title="添加用户" :visible.sync="addDialogFormVisible">
  <el-form :model="addForm" :rules="myRules" ref="addRef">
    <el-form-item label="用户名" label-width="90px" prop="username">
      <el-input v-model="addForm.username" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="密码" label-width="90px" prop="password">
      <el-input v-model="addForm.password" autocomplete="off"  type="password"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" label-width="90px" prop="emali">
      <el-input v-model="addForm.email" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="电话" label-width="90px" prop="moblie">
      <el-input v-model="addForm.mobile" autocomplete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="addDialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="addList">确 定</el-button>
  </div>
</el-dialog>
<!-- 编辑用户 -->
<el-dialog title="编辑用户" :visible.sync="editDialogFormVisible">
  <el-form :model="editForm" :rules="myRules" ref="editRef">
    <el-form-item label="用户名" label-width="90px" prop="username">
      <el-input v-model="editForm.username" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" label-width="90px" prop="emali">
      <el-input v-model="editForm.email" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="电话" label-width="90px" prop="moblie">
      <el-input v-model="editForm.mobile" autocomplete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="editDialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="editTxt">确 定</el-button>
  </div>
</el-dialog>
     <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[100, 200, 300, 400]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="400">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { getUserList, getAddList, delList, editList } from '@/api'
export default {
  data () {
    let chgEmail = (rule, value, cb) => {
      if (value === '') {
        cb(new Error('请输入邮箱'))
      } else {
        let emailReg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
        if (emailReg.test(value)) {
          cb()
        } else {
          cb(new Error('请输入正确的邮箱规则'))
        }
      }
    }
    return {
      searchVal: '',
      tableData: [],
      currentPage4: 4,
      addDialogFormVisible: false,
      addForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      editDialogFormVisible: false,
      editForm: {
        username: '',
        email: '',
        mobile: '',
        id: ''
      },
      myRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        email: [
          { required: true, validator: chgEmail, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ]
      },
      loading: true,
      value1: true,
      value2: true
    }
  },
  created () {
    this.initList()
  },
  methods: {
    initList () {
      this.loading = true
      getUserList({ query: this.searchVal, pagenum: 1, pagesize: 10 })
        .then(res => {
          // console.log(res)
          this.tableData = res.data.data.users
          this.loading = false
        })
    },
    handleSearch () {
      this.initList()
    },
    handleAdd () {
      this.addDialogFormVisible = true
    },
    addList () {
      this.$refs.addRef.validate(isPass => {
        if (isPass) {
          getAddList(this.addForm)
            .then(res => {
              console.log(res)
              if (res.data.meta.status === 201) {
                this.$message.success(res.data.meta.msg)
                this.addDialogFormVisible = false
                this.initList()
                this.addForm.username = ''
                this.addForm.password = ''
                this.addForm.email = ''
                this.addForm.mobile = ''
              } else {
                this.$message.error(res.data.meta.msg)
              }
            })
        } else {
          this.$message.error('请输入正确的信息')
        }
      })
    },
    deleteList (row) {
      // console.log(row)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        delList(row.id)
          .then(res => {
            if (res.data.meta.status === 200) {
              this.$message.success(res.data.meta.msg)
              this.initList()
            } else {
              this.$message.error(res.data.meta.msg)
            }
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    editUser (row) {
      this.editDialogFormVisible = true
      this.editForm.username = row.username
      this.editForm.email = row.email
      this.editForm.mobile = row.mobile
      this.editForm.id = row.id
    },
    editTxt () {
      editList(this.editForm)
        .then(res => {
          if (res.data.meta.status === 200) {
            this.$message.success(res.data.meta.msg)
            this.editDialogFormVisible = false
            this.initList()
            this.editForm.username = ''
            this.editForm.email = ''
            this.editForm.mobile = ''
          } else {
            this.$message.error(res.data.meta.msg)
          }
        })
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
    }
  }
}
</script>
<style lang="scss" scoped>
.bread {
  background-color: #F5F5F5;
  height: 45px;
  font-size: 15px;
  padding-left: 10px;
  line-height: 45px;
  margin-bottom: 15px;
}
  .search-input {
    width: 300px;
    margin-bottom: 15px;
  }
.block{
  background-color: #F5F5F5;
  margin-top: 15px;
}
</style>
